package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.FoodDao;
import com.ChenYaRou.www.dao.ShopDao;
import com.ChenYaRou.www.dao.impl.FoodDapImpl;
import com.ChenYaRou.www.dao.impl.ShopDaoImpl;
import com.ChenYaRou.www.po.Food;
import com.ChenYaRou.www.po.Shop;
import com.ChenYaRou.www.utils.JsonBean;
import com.alibaba.fastjson.JSON;

import java.sql.SQLException;
import java.util.*;

import static com.ChenYaRou.www.constant.False.EMPTY;

/**
 * @ClassName FuzzySelectShopController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/1 0:36
 * @Version 1.0
 **/
public class FuzzySelectShopServletController {
    private LinkedHashSet<Shop> shopLinkedList=new LinkedHashSet<>();
    private ShopDao shopDao=new ShopDaoImpl();
    /**
     * 模糊搜索 模糊关键字可以是店铺名 可以是店铺介绍  地区 街区 详细地址  可以是food的name
     * @param fuzzyWord
     * @return
     * 情况1 shops是空的 直接 返回错误，空空如也 找不到;
     * 情况2 shops是有的 但foods是空的 返回shops的json;   一般情况1 2 是不会发生的，因为数据库必然存有东西
     * 情况3 shops有 foods也有 则返回其绑定的shop—food json
     */
    public String fuzzySelectShop(String fuzzyWord){
        shopLinkedList.clear();
        if(fuzzyWord==null ||EMPTY.equals(fuzzyWord.trim())){
            return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
        }
        StringBuilder json=new StringBuilder();
        fuzzyWord=fuzzyWord.substring(1,fuzzyWord.length()-1);
        try{
            //获取所有shop
            List<Shop> shops = shopDao.selectAll(new Shop());
            //模糊关键字可以是店铺名 可以是店铺介绍 地区 街区 详细地址
            if(shops!=null && !shops.isEmpty()){
                for (Shop shop : shops) {
                    if(shop.getName().contains(fuzzyWord)){
                        shopLinkedList.add(shop);
                    }else if(shop.getIntroduction().contains(fuzzyWord)){
                        shopLinkedList.add(shop);
                    }else if(shop.getDistrictName().contains(fuzzyWord)){
                        shopLinkedList.add(shop);
                    }else if(shop.getStreetName().contains(fuzzyWord)){
                        shopLinkedList.add(shop);
                    }else if(shop.getDetailedAddress().contains(fuzzyWord)){
                        shopLinkedList.add(shop);
                    }
                }
                if(shopLinkedList.isEmpty()){
                    return JsonBean.generateJson(JsonBeanConstant.NOT_GET,JsonBeanConstant.NULL,null);
                }
                //获取所有food
                FoodDao foodDao=new FoodDapImpl();
                List<Food> foods = foodDao.selectAll(new Food());
                if(foods!=null && !foods.isEmpty()){
                    for (Food food : foods) {
                        //foodName中含有模糊字
                        if(food.getName().contains(fuzzyWord)){
                            for (Shop shop : shops) {
                                //food对应的shop添加上
                                if(shop.getId().equals(food.getShopId())){
                                    shopLinkedList.add(shop);
                                }
                            }
                        }
                    }
                    json.append("[");
                    //将所有shopLickedList与对应的food捆绑起来
                    HashMap<String,Object> hashMap=new HashMap<>(1024);
                    LinkedHashSet<Food> theShopFoods=new LinkedHashSet<>();
                    for (Shop shop : shopLinkedList) {
                        //获得shop对应的所有food 放在theShopFoods下
                        hashMap.put("Shop",shop);
                        for (Food food : foods) {
                            if(food.getShopId().equals(shop.getId())){
                                theShopFoods.add(food);
                            }
                        }
                        hashMap.put("Foods",theShopFoods);
                        json.append(JSON.toJSONString(hashMap)).append(",");
                        theShopFoods.clear();
                    }
                    json.deleteCharAt(json.lastIndexOf(",")).append("]");
                    //因为data过于复杂 于是手动json...
                    return "{\"code\":" + JsonBeanConstant.GET+",\"data\":" + json +
                            ",\"state\":\"" + JsonBeanConstant.SUCCESS+"\"}";
                }else{
                    return JsonBean.generateJson(JsonBeanConstant.GET,JsonBeanConstant.SUCCESS,shops);
                }
            }
        } catch (IllegalAccessException | SQLException |
                NoSuchFieldException | InstantiationException  e) {
            e.printStackTrace();
        }
        return JsonBean.operateList(JsonBeanConstant.NULL,null);
    }

}
